module breath_led (
    input sys_clk,
    input sys_rst_n,
    output reg [1:0] led
);

parameter CNT_MAX = 17'd100_000;
reg [16:0] cnt_num = 17'd0;
reg [16:0] cnt = 17'd0;
reg flag = 1'b0;

// 计数器计时
always @(posedge sys_clk or negedge sys_rst_n) begin
    if(!sys_rst_n)
        cnt <= 17'd0;
    else if(cnt < CNT_MAX)
        cnt <= cnt + 17'd1;
    else
        cnt <= 17'd0;
end

// 占比调整
always @(posedge sys_clk or negedge sys_rst_n) begin
    if(!sys_rst_n)
        cnt_num <= 17'd0;
    else if(cnt == CNT_MAX)
        if(flag)
            cnt_num <= cnt_num + 17'd100;
        else
            cnt_num <= cnt_num - 17'd100;
    else
        cnt_num <= cnt_num;
end

// 占比增加/减少标识符
always @(posedge sys_clk or negedge sys_rst_n) begin
    if(!sys_rst_n)
        flag <= 1'b0;
    else if(cnt_num == CNT_MAX)
        flag <= 1'b0;
    else if(cnt_num == 17'd0)
        flag <= 1'b1;
    else
        flag <= flag;
end

// led状态控制
always @(posedge sys_clk or negedge sys_rst_n) begin
    if(!sys_rst_n)
        led <= 2'b00;
    else if(cnt < cnt_num)
        led <= 2'b01;
    else if(cnt > cnt_num)
        led <= 2'b10;
    else begin
        led <= led;
    end
end

endmodule